//
//  RessetViewController.m
//  GoldGuide
//
//  Created by Ryan on 16/5/18.
//  Copyright © 2016年 Ryan. All rights reserved.
//

#import "RessetViewController.h"
#import "LPUtils.h"
#import "CommonService.h"
@interface RessetViewController ()
{
    NSTimer *timer1;
}
@property (weak, nonatomic) IBOutlet UIButton *submitBtn;
@property (weak, nonatomic) IBOutlet UIButton *btnSMS;
@end

@implementation RessetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = HEXCOLOR(0xededed);
    self.submitBtn.backgroundColor = GreenColor;
    self.submitBtn.layer.cornerRadius = 4;
    self.pwdTextField.secureTextEntry = YES;
}

- (IBAction)userResset:(id)sender
{
    if (![LPUtils isValidatePhone:self.mobileNumTextField.text]) {
        [self showTips:@"手机号码不正确"];
        return ;
    }
    if (!self.verifyTextField.text) {
        [self showTips:@"验证码为空"];
        return ;
    }
    
    NSDictionary *parm = @{@"mobile":self.mobileNumTextField.text,
                           @"pwd":[LPUtils CCSMD5: [LPUtils CCSMD5:self.pwdTextField.text]],
                           @"cvcode":self.verifyTextField.text,
                           };
    [self showLoadingView:YES];
    CommonService *service = [[CommonService alloc]init];
    [service getNetWorkData:parm Path:REGISTER_NEW Successed:^(id entity) {
        [self showLoadingView:NO];
        [self showTips:@"重置成功"];
        [self performSelector:@selector(popToLast) withObject:nil afterDelay:0.8];
    } Failed:^(NSInteger errorCode, NSString *message) {
        [self showLoadingView:NO];
        [self showTips:message];
        
    }];
}

- (void)popToLast
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)doBack:(id)sender {
    [self popToLast];
}

- (IBAction)doGetSMS:(id)sender {
    if ([LPUtils isValidatePhone:self.mobileNumTextField.text] == NO) {
        ALERT_ERR_MSG(TelEnterWrong);
    } else {
        self.btnSMS.enabled = NO;
        timer1 = [NSTimer scheduledTimerWithTimeInterval:1
                                                  target:self
                                                selector:@selector(showRepeatButton)
                                                userInfo:nil
                                                 repeats:YES];
        //        [btnSMS setBackgroundColor:[UIColor lightGrayColor]];
        [timer1 fire];
        [self getSMSData:self.mobileNumTextField.text];
    }
}

//- (IBAction)textFieldDidChange:(id)sender {
//    if (textFieldNumber.text.length == 0) {
//        [btnLogin setBackgroundColor:[UIColor colorWithRed:0.82 green:0.82 blue:0.82 alpha:1]];
//    } else {
//        [btnLogin setBackgroundColor:[UIColor colorWithRed:0.36 green:0.8 blue:0.27 alpha:1]];
//    }
//}

- (void) getSMSData:(NSString *)phoneNumber {
    
    CommonService *service = [[CommonService alloc] init];
    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:
                           phoneNumber, @"mobile", nil];
    
    [service getNetWorkData:param
                       Path:MOBILE_PHONE_SMS
                  Successed:^(id entity) {
                      NSDictionary *dic = (NSDictionary *)entity;
                      DebugLog(@"%@", dic);
                      
                      NSNumber *code = [dic objectForKey:@"code"];
                      NSString *msg = [dic objectForKey:@"msg"];
                      
                      if ([code intValue] != 0) {
                          ALERT_ERR_MSG(msg);
                      } else {
                          NSDictionary *verify_code = [dic objectForKey:@"Verify_code"];
                          NSNumber *numberVC = [verify_code valueForKeyPath:@"verify_code"];
                          //                          verifyCode = [NSString stringWithFormat:@"%d", [numberVC intValue]];
                      }
                  } Failed:^(NSInteger errorCode, NSString *message) {
                      DebugLog(@"%@", message);
                  }];
}

- (void) showRepeatButton {
    static int count = 0;
    if (count++>=60) {
        self.btnSMS.enabled = YES;
        [timer1 invalidate];
        count = 0;
        [self.btnSMS setTitle:@"获取验证码" forState:UIControlStateNormal];
        //        [self.btnSMS setBackgroundColor:[UIColor colorWithRed:0.55 green:0.77 blue:0.2 alpha:1]];
        return;
    }
    
    [self.btnSMS setTitle:[NSString stringWithFormat:@"%i秒可重发",60-count] forState:UIControlStateNormal];
}

@end
